package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.service.BlogService;
import com.itheima.service.CommentService;
import com.itheima.service.UserService;
import com.itheima.service.WebVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IndexController
 * @Description 首页 前端控制器
 * @Author pengkaka
 * @Date 2020/8/4 19:31
 * @Version 1.0
 **/
@RestController
@RequestMapping("/index")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class IndexController {

    @Autowired
    BlogService blogService;

    @Autowired
    CommentService commentService;

    @Autowired
    WebVisitService webVisitService;

    @Autowired
    UserService userService;

    // 首页初始化数据
    @GetMapping("/init")
    public Result init() {

        Map<String, Object> map = new HashMap<>();

        Integer blogCount = blogService.getBlogCount(1);
        Integer commentCount = commentService.getCommentCount(1);
        Integer userCount = userService.getUserCount(1);
        Integer visitCount = webVisitService.getWebVisitCount();

        map.put("blogCount", blogCount);
        map.put("commentCount", commentCount);
        map.put("userCount", userCount);
        map.put("visitCount", visitCount);

        return new Result("success", map);
    }

    // 获取一年内的文章贡献度
    @GetMapping("/getBlogContributeCount")
    public Result getBlogContributeCount() {
        Map<String, Object> resultMap = blogService.getBlogContributeCount();
        return new Result("success", resultMap);
    }

    // 获取每个分类下文章数目
    @GetMapping("/getBlogCountByBlogSort")
    public Result getBlogCountByBlogSort() {
        List<Map<String, Object>> resultList = blogService.getBlogCountByBlogSort();
        return new Result("success", resultList);
    }

    // 获取每个标签下文章数目
    @GetMapping("/getBlogCountByTag")
    public Result getBlogCountByTag() {
        List<Map<String, Object>> resultList = blogService.getBlogCountByTag();
        return new Result("success", resultList);
    }

    // 获取最近一周用户独立IP数和访问量
    @GetMapping("/getVisitByWeek")
    public Result getVisitByWeek() {
        Map<String, Object> resultMap = webVisitService.getVisitByWeek();
        return new Result("success", resultMap);
    }
}
